RStudio

When you open RStudio, you should see something like this:


There should be 3 different windows along with a number of tabs.

Left

This is the R console, where you key in commands to be run in an interactive fashion. Type in your command and hit the Enter key. Once you hit the Enter key, R executes your command and prints the result, if any.

Top-right

  • Environment: List of objects that we have created or have access to. We can also see the list of objects using the command ls().
  • History: List of commands that we have entered into the console.

Bottom-right

  • Files: Allows you to navigate the directory structure on your computer.
  • Plots: Any graphical output you make will be displayed here.
  • Packages: List of packages that have been installed on your computer.
  • Help: Documentation for functionName appears here when you type ?functionName in the console.
  • Viewer: For displaying local web content.

Top-left

There isn’t anything here at the moment, but this space will become useful later when we are working with scripts. Click the icon in the top-left corner of the window, and click “R Script”. A new window pane that looks like a text editor opens up.

We’ll explore scripts later in the course, but for now, this is a useful place for us to type out long commands (especially those which span over multiple lines). To execute code from this window, highlight the code and click the button at the top of the window (or Cmd-Enter on a Mac, Ctrl-Enter on Windows).

R as a calculator

You can use R has a high-powered calculator. For example,

1 + 2
## [1] 3
456 * 7
## [1] 3192
5 / 2
## [1] 2.5

Notice that the command 5/2 gave the result 2.5, while several other programming languages would typically give 2 as a result.

There are several math functions which come with R. For example, to evaluate \(log (e^{25} - 2^{\sin(\pi)})\), we would type

log(exp(25) - 2^(sin(pi)))
## [1] 25

Types of variables

Apart from numbers, R supports a number of different “types” of variables. The most commonly used ones are numeric variables, character variables (i.e. strings), factor variables, and boolean (or logical) variables.

We can check the type of a variable by using the typeof function:

typeof("1")
## [1] "character"
typeof(TRUE)
## [1] "logical"

We can change the type of a variable to type x using the function as.x. This process is called “coercion”. For example, the following code changes the number 6507232300 to the string "6507232300":

as.character(6507232300)
## [1] "6507232300"
typeof(6507232300)
## [1] "double"
typeof(as.character(6507232300))
## [1] "character"

We can also change variables to numbers or boolean variables.

as.numeric("123")
## [1] 123
as.logical(123)
## [1] TRUE
as.logical(0)
## [1] FALSE

Sometimes type conversion might not work:

as.numeric("def")
## Warning: NAs introduced by coercion
## [1] NA

Sometimes type conversion does not work as you might expect. Always check that the result is what you want!

as.logical("123")
## [1] NA

Variable assignment

Often, we want to store the result of a computation so that we can use it later. R allows us to do this by variable assignment. Variable names must start with a letter and can only contain letters, numbers, _ and ..

The following code assigns the value 2 to the variable x:

x <- 2

Do not use the = sign to assign values to variables! Although it works in R, it can cause a lot of confusion.

Notice that no output was printed. This is because the act of variable assignment doesn’t produce any output. If we want to see what x contains, simply key its name into the console:

x
## [1] 2

For more complex objects that will encounter soon, we can use the str function to get information on the internal structure of the object:

str(x)
##  num 2

We can use x in computations:

x^2 + 3*x
## [1] 10

We can also reassign x to a different value:

x <- x^2
x
## [1] 4

What is the value of x and y after I execute the following code?

y <- x
x <- x^2

Let’s add a third variable:

z <- 3

Note that we now have 3 entries in our Environment tab. To remove an object/variable, use the rm() function:

rm(x)

To remove more than one object, separate them by commas:

rm(y, z)

Let’s add the 3 variables back again:

x <- 1; y <- 2; z <- 3

To remove all objects at once, use the following code:

rm(list = ls())

Session info

This section is for documentation purposes: By displaying my session info, others who read this document will know what the system set-up was when I ran the commands above.

sessionInfo()
## R version 3.6.1 (2019-07-05)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS Mojave 10.14.5
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## loaded via a namespace (and not attached):
##  [1] compiler_3.6.1  magrittr_1.5    tools_3.6.1     htmltools_0.3.6
##  [5] yaml_2.2.0      Rcpp_1.0.2      stringi_1.4.3   rmarkdown_1.15 
##  [9] knitr_1.24      stringr_1.4.0   xfun_0.9        digest_0.6.20  
## [13] evaluate_0.14